package com.edu.exam.entity.examination.control;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import com.edu.exam.enums.examination.SessionState;
import com.edu.exam.enums.examination.NavigationMode;

/**
 * 答题控制会话实体类
 * 所属模块：E-01 考试执行模块
 * 对应数据库表：answer_control_session
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
public class AnswerControlSession {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 会话ID
     */
    @Column(name = "session_id", nullable = false, length = 32, unique = true)
    private String sessionId;

    /**
     * 学生ID
     */
    @Column(name = "student_id", nullable = false, length = 32)
    private String studentId;

    /**
     * 试卷ID
     */
    @Column(name = "exam_paper_id", nullable = false, length = 32)
    private String examPaperId;

    /**
     * 当前状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "current_state", nullable = false, length = 20)
    private SessionState currentState = SessionState.NOT_STARTED;

    /**
     * 当前题目ID
     */
    @Column(name = "current_question_id", length = 32)
    private String currentQuestionId;

    /**
     * 导航模式
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "navigation_mode", nullable = false, length = 20)
    private NavigationMode navigationMode = NavigationMode.SEQUENTIAL;

    /**
     * 创建者ID
     */
    @Column(name = "creator_id", nullable = false, length = 32)
    private String creatorId;

    /**
     * 最后修改者ID
     */
    @Column(name = "modifier_id", length = 32)
    private String modifierId;

    /**
     * 版本号
     */
    @Column(name = "version", nullable = false)
    private Integer version = 1;

    /**
     * 开始时间
     */
    @Column(name = "start_time")
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    @Column(name = "end_time")
    private LocalDateTime endTime;

    /**
     * 会话元数据
     */
    @Column(name = "session_metadata", columnDefinition = "TEXT")
    private String sessionMetadata;

    /**
     * 自定义参数
     */
    @Column(name = "custom_parameters", columnDefinition = "TEXT")
    private String customParameters;

    /**
     * 会话备注
     */
    @Column(name = "session_notes", columnDefinition = "TEXT")
    private String sessionNotes;

    /**
     * 是否允许暂停
     */
    @Column(name = "allow_pause")
    private Boolean allowPause = true;

    /**
     * 是否允许复习
     */
    @Column(name = "allow_review")
    private Boolean allowReview = true;

    /**
     * 是否允许标记
     */
    @Column(name = "allow_marking")
    private Boolean allowMarking = true;

    /**
     * 最后活动时间
     */
    @Column(name = "last_activity_time")
    private LocalDateTime lastActivityTime;

    /**
     * 暂停时长
     */
    @Column(name = "pause_duration")
    private Integer pauseDuration = 0;

    /**
     * 异常计数
     */
    @Column(name = "abnormality_count")
    private Integer abnormalityCount = 0;

    /**
     * 超时次数
     */
    @Column(name = "timeout_count")
    private Integer timeoutCount = 0;

    /**
     * 会话标签
     */
    @Column(name = "session_tags", columnDefinition = "TEXT")
    private String sessionTags;

    // ===== 审计字段 =====

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 是否删除
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted = false;
}